/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.container.report;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;

import org.activequant.core.domainmodel.account.BalanceEntry;
import org.activequant.core.domainmodel.account.OrderHistory;
import org.activequant.util.exceptions.ReportingException;
import org.activequant.util.tools.StringUtils;
import org.apache.log4j.Logger;


/**
 * This class is a simple text reporting service that dumps the trades
 * as well as the balance book entries into a file. <br>
 * An IReportingService implementation. Writes the report in a .txt file. Holds the following associated variable:
 * <ul>
 * <li>filepath(String)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [01.03.2007] Created (Erik Nijkamp)<br>
 *  - [22.02.2008] Refactoring reporting API (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 *  @author Ulrich Staudinger
 */
public class TextReportingService implements IReportingService {
	
	private final static Logger log = Logger.getLogger(TextReportingService.class);
	/**
	 * private String filepath;	
	 */
	private String filepath;
	
	/**
	 * returns the associated filepath(String)
	 * @return the filepath
	 */
	public String getFilepath() {
		return filepath;
	}

	/**
	 * sets the associated filepath(String) with the given filepath(String)
	 * @param filepath the filepath to set
	 */
	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	/**
	 * If the given report(IReport) is an AccountReport, it creates/writes a report .txt file from that AccountReport and writes it at the following location:
	 *  "${filepath}${report.getAccount.getHolder}.txt"
	 */
	public void renderReport(IReport report) throws ReportingException {
		// check report type
		if(report instanceof AccountReport) {
			AccountReport accountReport = (AccountReport) report;
			renderAccountReport(accountReport);
		} else {
			throw new ReportingException("Illegal report type.");
		}
	}
	/**
	 * creates/writes a report .txt file from the given report(AccountReport) and writes it at the following location:
	 *  "${filepath}${report.getAccount.getHolder}.txt"
	 * @param report
	 * @throws ReportingException
	 */
	public void renderAccountReport(AccountReport report) throws ReportingException {
		// log
		log.info("Rendering report.");
		checkFolder();
		
		// open
		PrintWriter out = null;
		try {
			out = new PrintWriter(filepath + report.getAccount().getHolder() + ".txt");
		} catch(Exception ex) {
			throw new ReportingException(ex);
		}
		out.println("Report from " + new Date() + ".\n");

		// cool method call :-)
		final String line = StringUtils.repeat('=', 90);
		// header
		{
			out.println(line);
			out.println("## Account " + report.getAccount().getHolder() + " ##");
			out.println(line);
			Iterator<String> keysIt = report.getReportValues().keySet().iterator();
			while (keysIt.hasNext()) {
				String key = keysIt.next();
				Object value = report.getReportValues().get(key);
				out.print(key);
				out.print(": ");
				out.println(value.toString());
			}

		}
		out.println(line);
		
		// dump out the balance book. 
		for(BalanceEntry be : report.getAccount().getBalanceBook().getBalanceEntries()){
			out.println(be.toString());
		}
		out.println(line);
		// dump out the executions
		
		// dump out the orders. 
		out.println(line);
		for(OrderHistory o : report.getAccount().getOrderBook().getClosedHistories()){
			out.println(o.toString());
		}
		
		out.println();
		out.close();
	}
	/**
	 * makes sure the associated filepath(String) location exists, and creates missing folders on the path if necessary
	 */
	private void checkFolder() {
		File folder = new File(filepath);
		if(!folder.exists()) folder.mkdir();
	}
}